#include "scenes/SceneNewFile.h"

#include "scenes/SceneMainMenu.h"
#include "dialogs/DialogFileSelect.h"

SceneNewFile::SceneNewFile( GameApplication& theApp )
   : App( theApp )
{
	// None
}

void SceneNewFile::Initialize()
{
	gdn::Font * font = App.Fonts.GetResource( App.GameResources[ "font sansation" ] );

	gdn::Image* filenameButtonImage = App.Images.GetResource( App.GameResources[ "gui button medium" ] );
	gdn::Image* extraLargeButtonImage = App.Images.GetResource( App.GameResources[ "gui button long" ] );

	keyboardUp = false;

	filenameText.SetColor( gdn::Color( 0, 0, 0 ) );
	filenameText.SetFont( *font );
	filenameText.SetCharacterSize( 18 );
	filenameText.SetString( "Filename: <none>" );
	filenameText.SetPosition( 10, 75 );

	filenameButton = gui::Button( gdn::Vector2f( 246, 75 ),
							      "Change", "Change", "Change",
							      gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				                  *filenameButtonImage, *font, 15,
				                  gdn::Rectanglei( 0, 0, 64, 32 ), gdn::Rectanglei( 64, 0, 64, 32 ), gdn::Rectanglei( 128, 0, 64, 32 ) );

	backButton = gui::Button( gdn::Vector2f( 25, 198 ),
						      "Back", "Back", "Back",
						      gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				              *extraLargeButtonImage, *font, 15,
				              gdn::Rectanglei( 0, 0, 122, 32 ), gdn::Rectanglei( 122, 0, 122, 32 ), gdn::Rectanglei( 244, 0, 122, 32 ) );

	nextButton = gui::Button( gdn::Vector2f( 173, 198 ),
						      "Next", "Next", "Next",
						      gdn::Color( 0, 0, 0 ), gdn::Color( 0, 0, 0 ), gdn::Color( 255, 255, 255 ),
				              *extraLargeButtonImage, *font, 15,
				              gdn::Rectanglei( 0, 0, 122, 32 ), gdn::Rectanglei( 122, 0, 122, 32 ), gdn::Rectanglei( 244, 0, 122, 32 ) );
}

void SceneNewFile::Terminate()
{
	// None
}

void SceneNewFile::Step( gdn::Window& window )
{
	gdn::Event Event;
	while ( window.GetEvent( Event ) )
	{
		if ( Event.type == gdn::Event::WindowClosed )
		{
			App.Close();
		}

		if ( filenameButton.Process( Event ) == GUI_BUTTON_RELEASE )
		{
			TextBoxInit();
			keyboardUp = true;
		}

		if ( keyboardUp and filenameGui.Process( Event ) == true )
		{
			chosenFilename = filenameGui.GetText();
			filenameText.SetString( "Filename: " + filenameGui.GetText() );
			keyboardUp = false;
		}
		else if ( !keyboardUp )
		{
			if ( backButton.Process( Event ) == GUI_BUTTON_RELEASE )
			{
				App.SetActiveScene( "Main Menu" );
				static_cast< SceneMainMenu* >( App.FindScene( App.GetActiveSceneName() ) )->OpenDialog< DialogFileSelect >();
			}

			if ( nextButton.Process( Event ) == GUI_BUTTON_RELEASE )
			{
				App.SetActiveScene( "Game" );
			}

			nextButton.Process( Event );
		}
	}
}

void SceneNewFile::Draw( gdn::Window& window )
{
	window.Clear();

	window.Draw( filenameText );
	filenameButton.Draw( window );

	if ( keyboardUp )
	{
		filenameGui.Draw( window );
	}
	else
	{
		backButton.Draw( window );
		nextButton.Draw( window );
	}

	window.Display();
}

void SceneNewFile::TextBoxInit()
{
	gdn::Font * font = App.Fonts.GetResource( App.GameResources[ "font sansation" ] );

	gdn::Image* filenameGuiImage = App.Images.GetResource( App.GameResources[ "gui textbox noshift" ] );
	gdn::Image* filenameGuiShiftImage = App.Images.GetResource( App.GameResources[ "gui textbox shift" ] );

	#include "scenes/SceneNewFile_TextBoxNoShift.inl"
	#include "scenes/SceneNewFile_TextBoxShift.inl"

	filenameGui = gui::TextBox( gdn::Vector2f( 0, 72 ), chosenFilename, 15,
			                    *filenameGuiImage, *filenameGuiShiftImage, *font, gdn::Color( 0, 0, 0 ), 23,
			                    gdn::Vector2f( 13, 12 ), charactersNoShift, charactersShift );
}

